//
//  LoginVC.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/10/15.
//  Copyright © 2018年 liu. All rights reserved.
//

#import "LoginVC.h"
#import "RegisterVC.h"
#import "loginView.h"
#import "ForgetPasswordVC.h"
#import "CheckVerificationPicView.h"

@interface LoginVC ()

@property (nonatomic, strong) LoginView *loginView;
@property (nonatomic, strong) CheckVerificationPicView *checkVeriPicView;
@property (nonatomic, strong) Liu70CountDownLabel *countDownLabel;

@end

@implementation LoginVC

#pragma mark - Construction Script
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)configSelf {
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)allocateComponents {
    self.view.fp_addSubview(self.loginView=[[LoginView alloc] init]);
    [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    self.view.fp_addSubview(self.countDownLabel = [[Liu70CountDownLabel alloc] init]);
    [self.countDownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.loginView.requestVerifyButton);
    }];
}

- (void)configComponents {
    self.loginView.loginButton.fp_addTargetActionForControlEvents(self, @selector(onLoginButtonTap), UIControlEventTouchUpInside);
    self.loginView.registerButton.fp_addTargetActionForControlEvents(self, @selector(onRegisterButtonTap), UIControlEventTouchUpInside);
    self.loginView.forgetButton.fp_addTargetActionForControlEvents(self, @selector(onForgetButtonTap), UIControlEventTouchUpInside);
    self.loginView.requestVerifyButton.fp_addTargetActionForControlEvents(self, @selector(onRequestVerifynButtonTap), UIControlEventTouchUpInside);
    self.countDownLabel.textColor = HexadecimalColor(@"2457B5");
}

#pragma mark - Events
- (void)onLoginButtonTap {
    [HandyMethods endEditing];
    [LBQRequestMobileLoginIndex goRequestWithPassword:self.loginView.passwordField.text mobile:self.loginView.accountField.text deviceType:DeviceType success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
        SaveUserToken(responseObject[@"token"]);
        [HandyMethods dispatchAfter:0.5 do:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    } failure:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
    }];
}

- (void)onRegisterButtonTap {
    [self.navigationController pushViewController:[[RegisterVC alloc] init] animated:YES];
}

- (void)onForgetButtonTap {
    ForgetPasswordVC *vc = [[ForgetPasswordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [HandyMethods endEditing];
}

- (void)onRequestVerifynButtonTap {
    if (self.loginView.accountField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请先填写手机号码！"];
        return;
    }
    
    [self.view addSubview:self.checkVeriPicView = [[CheckVerificationPicView alloc] init]];
    [self.checkVeriPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    self.checkVeriPicView.okButton.fp_addTargetActionForControlEvents(self, @selector(onPicVeriOKButtonTap), UIControlEventTouchUpInside);
}

- (void)onPicVeriOKButtonTap {
    [LBQRequestMobileLoginSendCode goRequestWithMobile:self.loginView.accountField.text
                                              img_code:self.checkVeriPicView.textField.text
                                                  type:LBQRequestMobileLoginSendCodeTypeLogin
                                             verify_id:self.checkVeriPicView.veriID
                                               success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                   self.loginView.requestVerifyButton.hidden = YES;
                                                   [self.countDownLabel startCountDownToEndDate:[NSDate dateWithTimeIntervalSinceNow:60] finish:^{
                                                       self.loginView.requestVerifyButton.hidden = NO;
                                                   }];
                                               }
                                               failure:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                   
                                               }];
    [self.checkVeriPicView removeFromSuperview];
}




@end
